package com.mcz.yun4book.controller;

import com.mcz.yun4book.vo.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("common")
public class CommonController {
    @Value("${upload.loc}")
    private String loc;
    @RequestMapping("/upload")
    @ResponseBody
    public Result upload(MultipartFile uimg){
        Result result=new Result<> ();
        //告诉文件保存的路径以及文件名
        //获取文件名
        String fileName= uimg.getOriginalFilename();
        try{
            String newFileName= UUID.randomUUID()+fileName.substring(fileName.lastIndexOf("."));
            result.setFileName(newFileName);
            uimg.transferTo(new File(loc+newFileName));
        }
        catch (IOException e){
            result.setCode(1);
            result.setMsg("上传失败");
            throw new RuntimeException(e);
        }

        return result;
    }
}
